{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0984cdd3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Euler's totient function phi(15): 8\n",
      "Euler's theorem: 7^phi(15) mod 15 = 1\n"
     ]
    }
   ],
   "source": [
    "def gcd(a, b):\n",
    "    while b:\n",
    "        a, b = b, a % b\n",
    "    return a\n",
    "\n",
    "def euler_phi(n):\n",
    "    count = 0\n",
    "    for i in range(1, n + 1):\n",
    "        if gcd(n, i) == 1:\n",
    "            count += 1\n",
    "    return count\n",
    "\n",
    "def euler_theorem(a, n):\n",
    "    if gcd(a, n) != 1:\n",
    "        raise ValueError(\"a and n must be coprime for Euler's theorem.\")\n",
    "    \n",
    "    result = pow(a, euler_phi(n), n)\n",
    "    return result\n",
    "\n",
    "# example\n",
    "n = 15\n",
    "print(f\"Euler's totient function phi({n}): {euler_phi(n)}\")\n",
    "# Euler's totient function phi(15): 8\n",
    "\n",
    "a = 7\n",
    "result = euler_theorem(a, n)\n",
    "print(f\"Euler's theorem: {a}^phi({n}) mod {n} = {result}\")\n",
    "# Euler's theorem: 7^phi(15) mod 15 = 1\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
